package com.administrator.scoreManageSys.controller.student;

import com.administrator.scoreManageSys.service.StudentService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;

/**
 * 学生管理控制类
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/student/information")
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping(value = "/list")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:list", logical = Logical.OR)
    public JSONObject list(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            Long classId = data.getLong("classId");
            String name = data.getString("name");
            return studentService.list(pageNum, pageSize, classId, name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取学生信息列表 异常");
        }
    }

    @PostMapping(value = "/resetPassword")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject resetPassword(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return studentService.resetPassword(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("重置密码 异常");
        }
    }
    @PostMapping(value = "/userStudentUpdate")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject userStudentUpdate(@RequestBody JSONObject data) {
        try {

            Long id = data.getLong("id");
            String name = data.getString("name");
            Long phone = data.getLong("phone");
            Long identity = data.getLong("identity");
            Long roleId = data.getLong("roleId");
            Long sex = data.getLong("sex");
            String identityCard = data.getString("identityCard");
            String idEntityCardAddress = data.getString("idEntityCardAddress");
            String address = data.getString("address");
            Long admissionTime = data.getLong("admissionTime");
            return studentService.userStudentUpdate(name, phone, roleId, address,id,identity,sex,identityCard,idEntityCardAddress,admissionTime);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改用户账户 异常");
        }
    }


    @PostMapping(value = "/getMyExamList")
    @RequiresRoles("studentScoreQuery")
    @RequiresPermissions(value = "score:list", logical = Logical.OR)
    public JSONObject getMyExamList(@RequestBody JSONObject data , HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Long season = data.getLong("season");
            Long subject = data.getLong("subject");
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            return studentService.getMyExamList(userId,season,subject,pageSize,pageNum);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取自己成绩 异常");
        }
    }

    @PostMapping(value = "/getUserDetail")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject getUserDetail(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return studentService.getUserDetail(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取学生详情信息 异常");
        }
    }
}
